package io.knifer.gokexceltask.excel.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import io.knifer.gokexceltask.utils.AESUtils;
import io.knifer.gokexceltask.utils.CellDataUtils;

import java.nio.charset.StandardCharsets;

public class IdCardNumberEncryptConverter implements Converter<String> {
    public final static byte[] KEY = "0123456789012345".getBytes(StandardCharsets.UTF_8);

    @Override
    public Class<?> supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public String convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return AESUtils.encryptToBase64(
                CellDataUtils.get(cellData).getBytes(StandardCharsets.UTF_8),
                KEY
        );
    }

    @Override
    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return new WriteCellData<>(AESUtils.decryptBase64ToPlainText(value, KEY));
    }
}
